//
//  TestViewController_1.swift
//  MZTool_Example
//
//  Created by gby on 2023/2/6.
//  Copyright © 2023 CocoaPods. All rights reserved.
//

import UIKit
import PromiseKit
import Alamofire
import MZTool
import RxSwift
import RxCocoa

class MGSUserModel: Decodable{
    func encode(with coder: NSCoder) {
        
    }
    
    required init?(coder: NSCoder) {
                
    }
    
    static var supportsSecureCoding: Bool = true
    
    var _id: String?
}


class TestViewController_1: BaseViewVCtrl {
    
    let disposebag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        
        do {
            let btn = UIButton.init(type: .custom)
            btn.setTitle("发发发", for: .normal)
            btn.titleLabel?.font = UIFont.mz.size(14)
            btn.setTitleColor(.black, for: .normal)
            btn.rx.tap.subscribe { event in
                self.action1()
            }.disposed(by: disposebag)
            btn.frame = CGRect(x: 50, y: 100, width: 100, height: 30)
            self.view.addSubview(btn)
        }
        
        
        do {
            let btn = UIButton.init(type: .custom)
            btn.setTitle("否是短发", for: .normal)
            btn.titleLabel?.font = UIFont.mz.size(14, weight:.medium)
            btn.setTitleColor(.black, for: .normal)
            btn.rx.tap.subscribe { event in
                self.action2()
            }.disposed(by: disposebag)
            btn.frame = CGRect(x: 50, y: 150, width: 100, height: 30)
            self.view.addSubview(btn)
        }
        
        do {
            let btn = UIButton.init(type: .custom)
            btn.setTitle("范德萨发情", for: .normal)
            btn.titleLabel?.font = UIFont.mz.size(14,weight: .bold)
            btn.setTitleColor(.black, for: .normal)
            btn.rx.tap.subscribe { event in
                self.action3()
            }.disposed(by: disposebag)
            btn.frame = CGRect(x: 50, y: 200, width: 100, height: 30)
            self.view.addSubview(btn)
        }
    }
    
    
    func action1() {
        let promise1 = Promise<Any>{ seal in
            TestApi.sharedProvidor.request(.getlist([:])) { result in
//                seal.reject(MZError.requestMapping("11111"))
                switch result{
                case .success(let resp):
                    seal.fulfill(resp)
                case .failure(let error):
                    seal.reject(error)
                }
            }
        }
        
        let promise2 = Promise<Any>{ seal in
            TestApi.sharedProvidor.request(.getlist([:])) { result in
                switch result{
                case .success(let resp):
                    seal.fulfill(resp)
                case .failure(let error):
                    seal.reject(error)
                }
            }
        }
        
        MZHUD.show(to: self.view)
        firstly {
            when(fulfilled: promise1, promise2)
        }.ensure { [weak self] in
            MZHUD.dissmiss(to: self?.view, immediately: true)
        }.done(on:.main , { (r1, r2) in
            MZ.printLog("r1 = \(r1), r2 = \(r2)")
        }).catch({ error in
            MZ.printLog(error)
        })
    }
    
    func action2() {
        let g1 = Guarantee<Any>{ seal in
            TestApi.sharedProvidor.request(.getlist([:])) { result in
                seal("1")
            }
        }
        let g2 = Promise<Any>{ seal in
            TestApi.sharedProvidor.request(.getlist([:])) { result in
                switch result{
                case .success(let resp):
                    seal.fulfill(resp)
                case .failure(let error):
                    seal.reject(error)
                }
            }
        }
        MZHUD.show(to: self.view)
        firstly {
            when(fulfilled: g1, g2)
        }.done(on:.main , { (r1, r2) in
            MZ.printLog("r1 = \(r1), r2 = \(r2)")
        }).catch({ error in
            MZ.printLog(error)
        })
    }
    
    
    func action3() {
        let promise1 = Promise<Any>{ seal in
            TestApi.sharedProvidor.request(.getlist([:])) { result in
                seal.reject(MZError.requestMapping("11111"))
            }
        }
        firstly {
            promise1
        }.recover({ error in
            return Promise<Any>{ seal in
                TestApi.sharedProvidor.request(.getlist([:])) { result in
                    seal.reject(MZError.requestMapping("11111"))
                }
            }
        }).done({ val in
            MZ.printLog("r1 = \(val)")
        }).catch { error in
            MZ.printLog(error)
        }
        
        self.navigationController?.pushViewController(Create.init(), animated: true)
    }
}
